'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: 10_json_output_parse.py
* @Time: 2025/10/22
* @All Rights Reserve By Brtc
'''
import dotenv
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from pydantic import BaseModel, Field

dotenv.load_dotenv()

class Jocker(BaseModel):
    jocker_name:str = Field(description ="回答用户的冷笑话")
    punch_line: str  =Field(description="这个冷笑话的笑点在哪里?")

parser = JsonOutputParser(pydantic_object=Jocker)

llm = ChatOpenAI(model="gpt-4o-mini")
prompt = (ChatPromptTemplate.from_template("请根据用户的问题进行回答。\n {format_instruction}\n{query}")
          .partial(format_instruction = parser.get_format_instructions()))

print(prompt)

joke = parser.invoke(llm.invoke(prompt.invoke({"query":"请讲一个关于程序员了冷笑话"})))
print(type(joke))
print(joke.get("punch_line"))
print(joke)